package cn.cvs.servlet.sysRole;

import cn.cvs.service.SysRoleService;
import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DelRoleServlet", urlPatterns = "/sys/role/del")
public class DelRoleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SysRoleService sysRoleService= (SysRoleService) SpringUtil.getBean ( "sysRoleService" );
        SysUserService userService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        PrintWriter writer = response.getWriter ( );
        String id = request.getParameter ( "roleId" )!=null?request.getParameter ( "roleId" ):"0";
        Map map=new HashMap<> (  );
        if (sysRoleService.getSysRoleById ( Integer.valueOf ( id ) )!=null){
            if (userService.getCount ( Integer.parseInt ( id ),null )<=0){
                List<Integer> ids=new ArrayList<> (  );
                ids.add ( Integer.valueOf ( id ) );
                int delete = sysRoleService.delete ( ids );
                if (delete>0){
                    map.put ( "delResult","true" );
                }else {
                    map.put ( "delResult","false" );
                }
            }else {
                map.put ( "delResult","false" );
            }
        }else {
            map.put ( "delResult","notexist" );
        }
        writer.write ( JSON.toJSONString(map) );
        writer.close ();
    }
}
